package com.superc.client.notebook;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.datepicker.client.DateBox;
import com.superc.client.MySuperToolboxService;
import com.superc.client.data.NotebookItem;
import com.superc.shared.ui.DateTimeBoxEx;
import com.superc.shared.ui.DialogBoxExt;
import com.superc.shared.ui.RichTextToolbar;

public class NotebookEditDialog extends DialogBoxExt {

	private ListBox comboBox=null;
	private DateTimeBoxEx dtStartTime =null;
	private DateTimeBoxEx dtEndTime =null;
	private RichTextArea txtContent =null;
	private RichTextToolbar tbrRichEdit =null;
	public NotebookEditDialog() {
		super(new HTML("<b>X</b>"));
		super.setHTML("编辑记事本");
		setSize("500px", "600px");
		txtContent = new RichTextArea();
		txtContent.setSize("100%", "100%");
		tbrRichEdit = new RichTextToolbar(txtContent);
		tbrRichEdit.setWidth("100%");
		tbrRichEdit.setHeight("30px");
		Grid gridLayout = new Grid(3, 1);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		gridLayout.setWidget(0, 0, horizontalPanel);
		gridLayout.getCellFormatter().setHeight(0, 0, "10px");
		gridLayout.getCellFormatter().setWidth(0, 0, "100%");
		
		PushButton cmdSave = new PushButton("");
		cmdSave.getUpFace().setImage(new Image("image/save.png"));
		cmdSave.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				MySuperToolboxService.Util.getInstance().addNotebookItem(
						txtContent.getText(),
						txtContent.getHTML(), 
						comboBox.getValue(comboBox.getSelectedIndex()), 
						dtStartTime.getValue(),
						dtEndTime.getValue(),
						new AsyncCallback<String>(){

							@Override
							public void onFailure(Throwable caught) {
								
								Window.alert("Fail");
							}

							@Override
							public void onSuccess(String result) {
								Window.alert("ok");
							}
					
				});
				
			}
			
		});
		horizontalPanel.add(cmdSave);
		horizontalPanel.setCellVerticalAlignment(cmdSave, HasVerticalAlignment.ALIGN_MIDDLE);
		
		PushButton cmdBrowser = new PushButton("");
		cmdBrowser.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				MySuperToolboxService.Util.getInstance().getNotebookItem("",0,0,
						new AsyncCallback<NotebookItem[]>(){

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Fail");
							}

							@Override
							public void onSuccess(NotebookItem[] result) {
								
								if(result!=null){
									txtContent.setHTML(result[0].getContent());
								}
								Window.alert("OK");
							}
				});
			
			}
		});
		cmdBrowser.getUpFace().setImage(new Image("image/browse.gif"));
		horizontalPanel.add(cmdBrowser);
		horizontalPanel.setCellVerticalAlignment(cmdBrowser, HasVerticalAlignment.ALIGN_MIDDLE);
		
		comboBox = new ListBox();
		comboBox.addItem("默认类别");
		comboBox.addItem("其他类别");
		horizontalPanel.add(comboBox);
		horizontalPanel.setCellVerticalAlignment(comboBox, HasVerticalAlignment.ALIGN_MIDDLE);
		
		Label label = new Label("开始时间");
		label.setWordWrap(false);
		horizontalPanel.add(label);
		horizontalPanel.setCellVerticalAlignment(label, HasVerticalAlignment.ALIGN_MIDDLE);
		
		dtStartTime = new DateTimeBoxEx();
		horizontalPanel.add(dtStartTime);
		horizontalPanel.setCellVerticalAlignment(dtStartTime, HasVerticalAlignment.ALIGN_MIDDLE);
		dtStartTime.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss")));
		Label label_1 = new Label("结束时间");
		label_1.setWordWrap(false);
		horizontalPanel.add(label_1);
		horizontalPanel.setCellVerticalAlignment(label_1, HasVerticalAlignment.ALIGN_MIDDLE);
		
		dtEndTime = new DateTimeBoxEx();
		horizontalPanel.add(dtEndTime);
		horizontalPanel.setCellVerticalAlignment(dtEndTime, HasVerticalAlignment.ALIGN_MIDDLE);
		gridLayout.setWidget(1, 0, tbrRichEdit);
		gridLayout.getCellFormatter().setHeight(1, 0, "10px");
		gridLayout.getCellFormatter().setWidth(1, 0, "100%");
		gridLayout.setWidget(2, 0, txtContent);
		gridLayout.getCellFormatter().setHeight(2, 0, "100%");
		gridLayout.getCellFormatter().setWidth(2, 0, "100%");
		gridLayout.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_TOP);
		gridLayout.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT);
		gridLayout.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_LEFT);
		gridLayout.getCellFormatter().setVerticalAlignment(2, 0, HasVerticalAlignment.ALIGN_TOP);
		setWidget(gridLayout);
		gridLayout.setSize("500px", "600px");
		gridLayout.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
		gridLayout.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		
		
	}

}
